<template>
  <div class="channel-edit">
      <van-cell :border="false">
        <div slot="title" class="title-text">我的频道</div>
        <van-button
        class="edit-btn"
        type="danger"
        plain
        round
        size="mini"
        @click="isEdit = !isEdit"
        >{{ isEdit ? '完成' : '编辑'}}</van-button>
      </van-cell>
    <van-grid class="my-grid" :gutter="10">
    <van-grid-item
    class="grid-item"
    v-for="(channel,index) in myChannels"
    :key="index"
    @click="onmygrid(channel, index)"
    >
    <van-icon
    slot="icon"
    name="clear"
    v-show="isEdit && !fiexChannels.includes(channel.id)"
    ></van-icon>
    <span
    class="text"
    :class="{ active: index === active }"
    slot="text"
    >{{ channel.name }}</span>
    </van-grid-item>
    </van-grid>
    <!-- 频道推荐 -->
      <van-cell :border="false">
        <div slot="title" class="title-text">频道推荐</div>
      </van-cell>
      <van-grid class="recommend-grid" :gutter="10">
        <van-grid-item
        class="grid-item"
        v-for="(channel, index) in recommendChannels"
        :key="index"
        icon="plus"
        :text="channel.name"
        @click="onAddChannel(channel)"
        />
      </van-grid>
  </div>
</template>

<script>
import { getAllChannels, addUserChannel, deleteUserChannel } from '@/api/channel'
import { setItem } from '@/uitls/storage.js'
import { mapState } from 'vuex'
export default {
  data () {
    return {
      allChannels: [], // 所有频道
      isEdit: false, // 控制编辑状态的显示
      fiexChannels: [0] // 固定频道,不能删
    }
  },
  name: 'ChannelEdit',
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  computed: {
    // 获取user
    ...mapState(['user']),
    recommendChannels () {
      const channels = []
      this.allChannels.forEach(channel => {
        const ret = this.myChannels.find(myChannel => {
          return myChannel.id === channel.id
        })
        if (!ret) {
          channels.push(channel)
        }
      })
      return channels
    }
  },
  created () {
    this.loadAllChannels()
  },
  methods: {
    // 获取所有频道
    async loadAllChannels () {
      try {
        const { data } = await getAllChannels()
        this.allChannels = data.data.channels
      } catch (err) {
        this.$toast('数据获取失败')
      }
    },
    // 点击添加频道
    async onAddChannel (channel) {
      this.myChannels.push(channel)
      // 判断是否已登录,根据user的值
      if (this.user) {
        // 已登录
        try {
          await addUserChannel({
            id: channel.id, // 频道id
            seq: this.myChannels.length // 频道的序号
          })
          this.$toast('添加成功')
        } catch (err) {
          this.$toast('添加失败,请稍后重试')
        }
      } else {
        // 未登录
        setItem('channels', this.myChannels)
      }
    },
    onmygrid (channelItem, index) {
      if (this.isEdit) {
        if (this.fiexChannels.includes(index)) {
          return
        }
        // 编辑状态,执行删除
        if (index <= this.active) {
          // 激活的频道索引 -1
          this.$emit('update-active', this.active - 1, true)
        }
        this.myChannels.splice(index, 1)
        // 删除数据持久化
        this.deleteChannel(channelItem)
      } else {
        // 非编辑,执行切换频道
        this.$emit('update-active', index, false)
      }
    },
    // 定义 deleteChannel 方法
    async deleteChannel (channel) {
      try {
        if (this.user) {
          // 已登录
          await deleteUserChannel(channel.id)
        } else {
          // 未登录
          setItem('channel', this.myChannels)
        }
      } catch (err) {
        console.log(err)
        this.$toast('删除失败,请稍后重新')
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
    padding: 85px 0;
    .title-text {
        font-size: 32px;
        color: #333;
    }
    .edit-btn {
        width: 104px;
        height: 48px;
        font-size: 26px;
        color: #f85959;
        border: 1px solid #f85959;
    }
    /deep/.grid-item {
        width: 160px;
        height: 86px;
        .van-grid-item__content {
            white-space: nowrap;
            background-color: #f4f5f6;
            .van-grid-item__text, .text {
                font-size: 28px;
                color: #222;
                margin-top: 0;
            }
            .active {
                color: red;
            }
            .van-grid-item__icon-wrapper {
                position: unset;
            }
        }
    }
    /deep/ .my-grid {
        .grid-item {
            .van-icon-clear {
                position: absolute;
                right: -10px;
                top: -10px;
                font-size: 30px;
                color: #cacaca;
                z-index: 2;
            }
        }
    }
    /deep/.recommend-grid {
         .grid-item {
            .van-grid-item__content {
            flex-direction: row;
            .van-icon-plus {
              font-size: 28px;
              margin-right: 6px;
            }
        }
         }
    }
}
</style>
